package com.paiai.lslease.action.gateway;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.paiai.lslease.auto.model.AppSocket;
import com.paiai.lslease.base.AppResult;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.service.appuser.AppUserModuleService;

/** 
* @author chenmfa
* @version 创建时间：2017年10月20日 上午8:58:31 
* @description
*/
@SuppressWarnings("serial")
public class AppUserGatewayAction extends BaseAction{
  
  private static final Logger logger = LoggerFactory.getLogger(AppUserGatewayAction.class);
  @Autowired
  AppUserModuleService appUserModuleService;
  private String mobile;
  private Integer houseId;
  private String name;
  private String mac;
  private String wifiSsid;
  private String wifiPassword;
  public void addUserGateway(){
    AppResult result;
    if(null == mobile ){
      result =AppResult.newFailResult("添加用户不能为空");
    }else if(null == houseId){
      result =AppResult.newFailResult("房源不能为空");
    }else if(null == name){
      result =AppResult.newFailResult("插头名称不能为空");
    }else if(null == mac){
      result =AppResult.newFailResult("插头地址不能为空");
//    }else if(null == wifiSsid){
//      result =AppResult.newFailResult("WIFI名称不能为空");
//    }else if(null == wifiSsid){
//      result =AppResult.newFailResult("WIFI密码不能为空");
//    }
    }else{
      try{
        logger.info(request.getRequestURI()+",接收到的参数："+JSON.toJSONString(request.getParameterMap()));
        AppSocket socket =  new AppSocket();
        socket.setHouseId(houseId);
        socket.setMac(mac);
        socket.setName(name);
        socket.setWifiPassword(wifiPassword);
        socket.setWifiSsid(wifiSsid);
        result = appUserModuleService.addUserGateway(socket,mobile);
      }catch(Exception e){
        logger.error("添加失败",e);
        result =AppResult.newFailResult("添加失败");
      }
    }
    sendResultMsg(result);
  }
  
  public void updateGateWifi(){
    AppResult result;
    if(null == mac){
      result = AppResult.newFailResult("插头地址不能为空");
    }else if(null == wifiSsid){
      result = AppResult.newFailResult("WIFI名称不能为空");
    }else if(null == wifiPassword){
      result = AppResult.newFailResult("WIFI密码不能为空");
    }else{
      try{
        logger.info(request.getRequestURI()+",接收到的参数："+JSON.toJSONString(request.getParameterMap()));
        AppSocket socket =  new AppSocket();
        socket.setMac(mac);
        socket.setWifiPassword(wifiPassword);
        socket.setWifiSsid(wifiSsid);
        appUserModuleService.updateGateWifi(socket);
        result = AppResult.newSusscessResult("修改成功");
      }catch(Exception e){
        logger.error("添加失败",e);
        result =AppResult.newFailResult("添加失败");
      }
    }
    sendResultMsg(result);
  }
  
  /**
   * @description 删除网络插头
   */
  public void deleteGateway(){
    AppResult result;
    if(null == mac){
      result = AppResult.newFailResult("设备地址为空");
    }else{
      try{
        logger.info(request.getRequestURI()+",接收到的参数："+JSON.toJSONString(request.getParameterMap()));
        appUserModuleService.deleteGateWifi(mac);
        result = AppResult.newSusscessResult("删除成功");
      }catch(Exception e){
        logger.error("添加失败",e);
        result =AppResult.newFailResult("删除失败");
      }
    }
    sendResultMsg(result);
  }
  
  /**
   * @description 查询网络插头
   */
  public void queryUserGateway(){
    AppResult result;
    if(null == mobile){
      result = AppResult.newFailResult("用户为空");
    }else{
      try{
        logger.info(request.getRequestURI()+",接收到的参数："+JSON.toJSONString(request.getParameterMap()));
        result = appUserModuleService.queryUserSocket(mobile, null);
      }catch(Exception e){
        logger.error("添加失败",e);
        result =AppResult.newFailResult("删除失败");
      }
    }
    sendResultMsg(result);
  }
  
  public void setMobile(String mobile) {
    this.mobile = mobile;
  }
  public void setHouseId(Integer houseId) {
    this.houseId = houseId;
  }
  public void setName(String name) {
    this.name = name;
  }
  public void setMac(String mac) {
    this.mac = mac;
  }
  public void setWifiSsid(String wifiSsid) {
    this.wifiSsid = wifiSsid;
  }
  public void setWifiPassword(String wifiPassword) {
    this.wifiPassword = wifiPassword;
  }
  
}
